package com.aaa.inbound.controller;


import com.aaa.entity.inbound.Purchase;
import com.aaa.inbound.mapper.PurchaseMapper;
import com.aaa.inbound.service.PurchaseDetailService;
import com.aaa.inbound.service.PurchaseService;
import com.aaa.vo.R;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author aaa
 * @since 2020-11-25
 */
@RestController
@RequestMapping("/inbound/purchase")
@Api(description = "采购单管理")
public class PurchaseController {

    @Autowired
    private PurchaseService purchaseService;

    @Autowired
    private PurchaseDetailService detailService;

    @PostMapping("/searchPurchase/{page}/{limit}")
    @ApiOperation(value = "根据条件分页查询供应商")
    public R searchPurchase(@ApiParam(value = "当前页数") @PathVariable Integer page,
                        @ApiParam(value = "每页个数") @PathVariable Integer limit,
                        @ApiParam(value = "查询条件") @RequestBody Purchase purchase){
        Page<Purchase> purchasePage = purchaseService.searchPurchase(page, limit, purchase);
        return R.ok().message("分页查询成功").code(200).data("page",purchasePage);
    }

    @PostMapping("/addOrUpdPurchase")
    @ApiOperation(value = "新增/修改方法")
    public R addPurchase(@RequestBody Purchase purchase){
        purchase.setWarehouseid(41L);
        purchase.setWarehousename("默认仓库");
        boolean b = purchaseService.saveOrUpdate(purchase);
        if (b) {
            return R.ok().message("操作成功").code(200);
        }else {
            return R.error().message("操作失败").code(500);
        }
    }

    @PostMapping("/delPurchase/{id}")
    @ApiOperation(value = "根据ID删除")
    public R delPurchase(@PathVariable Long id){
        boolean b = purchaseService.removeById(id);
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("pid",id);
        detailService.remove(queryWrapper);
        if (b) {
            return R.ok().message("删除成功").code(200);
        }else {
            return R.error().message("删除失败").code(500);
        }
    }

    @PostMapping("/findSupplier/{warehouseid}")
    @ApiOperation(value = "供应商下拉查询")
    public List<Map> findSupplier(@ApiParam(value = "仓库ID") @PathVariable Long warehouseid){
        return purchaseService.findSupplier(warehouseid);
    }


}

